package chapter1.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DButil {
	private static final Logger LOGGER=LoggerFactory.getLogger(DButil.class);
	
	private static final String DRIVER;
	private static final String URL;
	private static final String USERNAME="";
	private static final String PASSWORD="";
	
	static{
		Properties prop =PropsUtil.loadProps(ConfigConstant.DATABASE_FILE);
		DRIVER=prop.getProperty(ConfigConstant.JDBC_DRIVER);
		URL=prop.getProperty(ConfigConstant.JDBC_URL);
		
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			LOGGER.error("load jdbc driver fails");
		}
	}
	//获取数据库连接
	public static Connection getConnection(){
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(URL);
		} catch (Exception e) {
			LOGGER.error("get Connection fails");
		}
		return conn;
	}
	//关闭数据库连接
	public static void closeConnection(Connection conn){
		if(conn!=null){
			try {
				conn.close();
			} catch (Exception e) {
				LOGGER.error("close Connection fails");
			}
		}
	}

}
